package com.pfsoft.producer.task;

import com.pfsoft.producer.common.TopicName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.SuccessCallback;

import java.time.LocalDateTime;

/**
 * @author pengfei
 * @date 2022年07月06日 16:12
 */

@Component
public class ProducerMessageTask {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Scheduled(fixedRate = 1000*60)
    public void createMessage(){

        kafkaTemplate.send(TopicName.TEST,"testKey"+ LocalDateTime.now(),"testValue").addCallback(new SuccessCallback<SendResult<String, String>>() {
            @Override
            public void onSuccess(SendResult<String, String> result) {
                System.out.println("send success");
            }
        }, new FailureCallback() {
            @Override
            public void onFailure(Throwable ex) {
                System.out.println("send failure");
            }
        });
    }
}
